package com.neuedu.jdbc.demo1;

import java.sql.*;
import java.util.Date;

/***
 * 项目名称:  MyJDBC 
 * 包：  com.neuedu.jdbc.demo1 
 * date:   2025/11/7   9:42  
 * 描述:  
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class UseJDBC {


    /***
     * - 加载驱动（jdbc的高版本可以省略）
     *   - Class.forname
     * - 建立连接
     *   - Drivermanager.getConnection(url,ussername,password)
     * - 创建Statement
     *   - executeQuery
     *   - executeUpdate
     * - 执行sql语句
     *   - select   --》返回结果集 ResultSet
     *   - inser、update、delete  ->  返回影响行数
     * -  处理结果集
     *   - 逐行的读取Rs
     * - 关闭资源
     * @param args
     */
    public static void main(String[] args) {

        // String url = "http://www.baidu.com";
        String url = "jdbc:mysql://localhost:3306/his?useUnicode=true&characterEncoding=utf8";
        String user  = "root";
        String password  = "root";


        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        // 旧版  5.xx-
        // Class.forName("com.mysql.jdbc.Driver");
        //8.x +
        try {
            // 1 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");


            //2 创建连接
            conn = DriverManager.getConnection(url, user, password);

            //3 创建语句块stmt
            stmt = conn.createStatement();

            //4 执行sql 语句
            String sql = "select  * from user   ";
            rs = stmt.executeQuery(sql);

            //解析结果集
            while(rs.next()){
                long id = rs.getLong("id");
                String username = rs.getString("username");
                String pd = rs.getString("password");
                String nickname = rs.getString("nickname");
                Date lastLogin = rs.getTimestamp("last_login");
                String status = rs.getString("STATUS");
                String createtime = rs.getString("create_time");
                Long createuser = rs.getLong("create_user");
                Date updateTime = rs.getTimestamp("update_time");
                Long updateuser = rs.getLong("update_user");

                System.out.println(String.format(
                        "id = %s\tusername = %s\tpd = %s\tnickname = %s\tlastLogin = %s\tstatus = %s\tcreatetime = %s\tcreateuser = %s\tupdateTime = %s\tupdateuser = %s\r\n",
                        id, username, pd, nickname, lastLogin, status, createtime, createuser, updateTime, updateuser
                ));

            }




        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭资源

            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

            if(stmt != null){
                try {
                    stmt.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }


            if(conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }
}
